package com.kexun.controller;


import com.kexun.common.PageResult;
import com.kexun.common.utils.ResponseEntity;
import com.kexun.entity.DO.Permission;
import com.kexun.service.PermissionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("system/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @GetMapping("list")
    public String tolist() {
        return "permission/permission_list";
    }

    @ResponseBody
    @PostMapping("list")
    public PageResult list() {
        return new PageResult(permissionService.findList(), 0);
    }

    @GetMapping("add")
    public String add(Model model) {
        List<Permission> permissionList = permissionService.findPermissionList();
        model.addAttribute("list", permissionList);
        return "permission/permission_add";
    }

    @ResponseBody
    @PostMapping("add")
    public ResponseEntity add(Permission permission) {
        System.out.println(permission);
        permissionService.add(permission);
        return new ResponseEntity().success();
    }

    @ResponseBody
    @PostMapping("del")
    public ResponseEntity del(int id) {
        int i = permissionService.deleteById(id);
        if (i > 0) {
            return new ResponseEntity().success("删除成功");
        } else {
            return new ResponseEntity().error("记录不存在");
        }
    }

    @GetMapping("edit")
    public String edit(int id, Model model) {
        List<Permission> permissionList = permissionService.findPermissionList();
        model.addAttribute("list", permissionList);
        Permission permission = permissionService.findPermissionById(id);
        model.addAttribute("permission", permission);
        return "permission/permission_edit";
    }

    @ResponseBody
    @PostMapping("edit")
    public ResponseEntity edit(Permission permission) {
        System.out.println(permission);
        permissionService.edit(permission);
        return new ResponseEntity().success();
    }


    @ResponseBody
    @PostMapping("tree")
    public ResponseEntity tree(int roleId) {
        List<Permission> permissions = permissionService.checkPermissionList(roleId);
        List<Map<String, Object>> tree = createTree(permissions, 0);
        return new ResponseEntity().success("success", tree);
    }

    @ResponseBody
    @PostMapping("hasTree")
    public ResponseEntity hasTree(int roleId) {
        List<Integer> hasIds = permissionService.getHasIds(roleId);
        return new ResponseEntity().success("success", hasIds);
    }


    @ResponseBody
    @PostMapping("allTree")
    public ResponseEntity allTree() {
        List<Permission> permissions = permissionService.permissionList();
        List<Map<String, Object>> tree = createTree(permissions, 0);
        return new ResponseEntity().success("success", tree);
    }


    private List<Map<String, Object>> createTree(List<Permission> permissionList, int pid) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Permission permission : permissionList) {
            if (permission.getParentId() == pid) {
                HashMap<String, Object> tree = new HashMap<>();
                tree.put("title", permission.getMenuName());
                tree.put("id", permission.getId());
                tree.put("checked", permission.isCheck());
                if (permission.getCount() > 0) {
                    tree.put("children", createTree(permissionList, permission.getId()));
                }
                list.add(tree);
            }
        }
        return list;
    }

}
